import functionnodes.FunctionNode;
import data.Value;

/**
 * @author loaner
 */
class functionnodes.FunctionNodeBqlist extends FunctionNode {
	
	function FunctionNodeBqlist() {
		super();
		funcname = "bqlist";
		type = SPECIALFUNCTION;
	}

	public function reduceXMLtoValue( scope:Object, XMLargs:Array ):Value
	{
		// much like list, this function creates a real XMLNode
		// and wraps it in a Value of XMLNODE type to return it.
		
		// the only difference is, this is a SPECIALFUNCTION
		// because, while list receives only args that have been eval'ed into Values,
		// this one receives UNevaluated XML nodes
		// some of which may or may not need evaluation
		// (they get eval'ed only if the have a <UQ> node wrapping them)
		
		
		//TODO: Make sure that when we evaluate an XMLNode
		 // we see if it's backquoted.
		 // if it is, we need to do two things:
		 // we need to clone its firstChild,
		 // and then we need to go through that child
		 // and see if there's anything unquoted in there.
		 /*
		  * for example, in Lisp, you could do
		  * (setf varname 'three)
		  * `(one two ,varname)
		  * and get
		  * (one two three)
		  * 
		  * so I need a function called something like resolveUnquotes()
		  * that receives an XMLNode and a scope
		  * and, if it is an <uq> node, resolves itself in Evaluator via convertXMLtoValue
		  * and replaces itself with that value's getXML() method,
		  * 
		  * or else, if it has childNodes,
		  * recursively calls resolveUnquotes on all its childNodes
		  * 
		  */
		
		var returnvalue:Value;
		
		var temp:XML = new XML( "<list></list>");
		
		
		for( var i:Number = 0; i < XMLargs.length; i++ )
		{
			var newnode:XMLNode = XMLargs[ i ].cloneNode( true );
			// for now, we'll completely sidestep the evaluation of nested <uq> nodes!
			temp.firstChild.appendChild( newnode );
		}
		
		var tempnode:XMLNode = temp.firstChild; 
		trace("The XML I'm returning is " + tempnode );
		
		returnvalue = new Value( Value.XMLNODE, tempnode );
		
		
		return returnvalue;	
	}
	
	private function findAndReplaceUnquotes( x:XMLNode )
	{
		// if it has childNodes, then check and replace them if they're unquoted.
		// then send each child to this function as well.	
	}

}